package bigo.todolist.avocado;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.MotionEvent;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

/**
 * A java class to deal with the longpress listener
 * @authorAlex Irlik, Steve Fox, TiOluwa Olarewaju, Derek Miller
 * Big-Ovacado Task Force
 * Version 1.0
 *
 */
public class BigMapView extends MapView {
	private List<Overlay> listOfOverLays;
	
	public interface OnLongpressListener {
        public void onLongpress(MapView view, GeoPoint longpressLocation);
    }
	
	static final int LONGPRESS_THRESHOLD = 500;
	
	private GeoPoint lastMapCenter;
	 
    private Timer longpressTimer = new Timer();
    private BigMapView.OnLongpressListener longpressListener;

    /**
     * Set BigMapView
     * @param context
     * @param apiKey
     */
	public BigMapView(Context context, String apiKey) {
		super(context, apiKey);
		// TODO Auto-generated constructor stub
	}

	/**
	 * Set BigMapView (override constructor)
	 * @param context
	 * @param attrs
	 */
	public BigMapView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	/**
	 * Set BigMapView (override constructor)
	 * @param context
	 * @param attrs
	 * @param defStyle
	 */
	public BigMapView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}
	
	/**
	 * Set on longpress listener
	 * @param listener
	 */
	public void setOnLongpressListener(BigMapView.OnLongpressListener listener) {
        longpressListener = listener;
    }
	
	/**
	 * Touch event
	 */
    public boolean onTouchEvent(MotionEvent event) {
        handleLongpress(event); //Just send it to longpress handler
 
        return super.onTouchEvent(event);
    }
	

	/**
	 * How to handle the long press of a user
	 * @param event
	 */
	private void handleLongpress(final MotionEvent event) {
		 
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            // Finger has touched screen.
            longpressTimer = new Timer();
            longpressTimer.schedule(new TimerTask() {
                public void run() {
                    GeoPoint longpressLocation = getProjection().fromPixels((int)event.getX(),
                            (int)event.getY());
 
                    /*
                     * Fire the listener. We pass the map location
                     * of the longpress as well, in case it is needed
                     * by the caller.
                     */
                    longpressListener.onLongpress(BigMapView.this, longpressLocation);

                }
 
            }, LONGPRESS_THRESHOLD);
 
            lastMapCenter = getMapCenter();
        }
 
        if (event.getAction() == MotionEvent.ACTION_MOVE) {
 
            if (!getMapCenter().equals(lastMapCenter)) {
                // User is panning the map, this is no longpress
                longpressTimer.cancel();
            }
 
            lastMapCenter = getMapCenter();
        }
 
        if (event.getAction() == MotionEvent.ACTION_UP) {
            // User has removed finger from map.
            longpressTimer.cancel();
        }
 
            if (event.getPointerCount() > 1) {
                        // This is a multitouch event, probably zooming.
                longpressTimer.cancel();
            }
    }
}



